# -*- coding: utf-8 -*-

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://doc.scrapy.org/en/latest/topics/item-pipeline.html
from pymongo import MongoClient

class GuokePipeline(object):
    def open_spider(self, spider):
        # 爬虫开启的时候执行一次
        client = MongoClient()
        self.collection = client["db"]["collection"]
        # 如果要在爬虫中使用此处的collection， 那么需要使用spider
        # spider.collection = client["db"]["collection"]  # 可以在spiders包中使用self.collection

    def process_item(self, item, spider):
        self.collection.insert_one(item)
        return item

    def close_spider(self, spider):
        # 爬虫关闭的时候执行一次
        # 对于需要关闭的 文件
        pass
